package com.qire.manhua.model.viewModel.impl;

import android.view.View;

import com.qire.common.basic.StringUtil;
import com.qire.common.widget.adapter.auto.AutoVariable;
import com.qire.common.widget.adapter.auto.EventReceiver;
import com.qire.common.widget.adapter.auto.SummerRecyclerBindAdapter;
import com.qire.common.widget.adapter.auto.annotation.AdapterEntityBind;
import com.qire.manhua.R;
import com.qire.manhua.databinding.LayoutHomeRecommendModuleRecordBinding;
import com.qire.manhua.databinding.LayoutHomeRecommendModuleRecordItemBinding;
import com.qire.manhua.model.entity.HomeModuleEntity;
import com.qire.manhua.model.entity.HomeModuleEntity.HomeModuleItemEntity;
import com.qire.manhua.model.enumeration.HomeModuleType;

import java.util.ArrayList;

import androidx.databinding.BaseObservable;
import androidx.databinding.Bindable;
import androidx.recyclerview.widget.RecyclerView.Adapter;

/**
 * 首页 -> 推荐 -> 记录列表模块
 * 用于构建组合首页复合布局中的记录列表模块，记录列表模块 {@link HomeModuleType#buildModuleViewModels HomeModuleType} 根据集体类型来构建。
 * @see HomeModuleType#TYPE_RECORD
 */
@AdapterEntityBind(viewRid = R.layout.layout_home_recommend_module_record)
public class RecommendModuleRecordViewModel extends BaseObservable implements AutoVariable<LayoutHomeRecommendModuleRecordBinding>, EventReceiver {

    private final HomeModuleEntity homeModuleEntity;

    private final ArrayList<ModuleRecordItemViewModel> recordItemViewModelList = new ArrayList<>();
    private final SummerRecyclerBindAdapter<ModuleRecordItemViewModel> recordAdapter = new SummerRecyclerBindAdapter(recordItemViewModelList);

    public RecommendModuleRecordViewModel(HomeModuleEntity homeModuleEntity) {
        this.homeModuleEntity = homeModuleEntity;
        ArrayList<HomeModuleItemEntity> moduleItemList = homeModuleEntity.moduleItemList();
        for(HomeModuleItemEntity moduleItemEntity : moduleItemList) {
            int serialNumber = moduleItemEntity.needShowSerial ? recordItemViewModelList.size() + 1 : -1;
            recordItemViewModelList.add(new ModuleRecordItemViewModel(moduleItemEntity, serialNumber));
        }
    }

    @Override
    public void bindVariable(LayoutHomeRecommendModuleRecordBinding viewBind) {
        viewBind.setRecommendModuleRecordViewModel(this);
    }

    @Override
    public void onReceiver(AutoVariable eventProcess) {
        recordAdapter.setEventProcess(eventProcess);
    }

    @Bindable
    public CharSequence getModuleTitle() {
        return homeModuleEntity.name;
    }

    @Bindable
    public CharSequence getTileBarMoreBtnText() {
        // todo: 特殊情况所以写死了，理论上应该交给外部处理，但是这个情况仅此一个， 998ID的模块就代表阅读记录，故简单处理
        return homeModuleEntity.id == 998 ? "阅读记录" : "更多榜单";
    }

    public Adapter getRecordAdapter() {
        return recordAdapter;
    }

    @AdapterEntityBind(viewRid = R.layout.layout_home_recommend_module_record_item)
    public class ModuleRecordItemViewModel extends BaseObservable implements AutoVariable<LayoutHomeRecommendModuleRecordItemBinding> {

        private final int[] serialNumberBgRid = {R.drawable.app_home_recommend_module_record_icon_1,
                R.drawable.app_home_recommend_module_record_icon_2,
                R.drawable.app_home_recommend_module_record_icon_3,
                R.drawable.app_home_recommend_module_record_icon_4};

        private final HomeModuleItemEntity moduleItemEntity;

        private final int serialNumber;

        public ModuleRecordItemViewModel(HomeModuleItemEntity moduleItemEntity, int serialNumber) {
            this.moduleItemEntity = moduleItemEntity;
            this.serialNumber = serialNumber;
        }

        @Override
        public void bindVariable(LayoutHomeRecommendModuleRecordItemBinding viewBind) {
            viewBind.setModuleRecordItemViewModel(this);
        }

        @Bindable
        public HomeModuleItemEntity getModuleItemEntity() {
            return moduleItemEntity;
        }

        @Bindable
        public String getCoverPath() {
            return moduleItemEntity.coverImg;
        }

        @Bindable
        public int getSerialNumberVisible() {
            return serialNumber > 0 ? View.VISIBLE : View.GONE;
        }
        @Bindable
        public CharSequence getSerialNumberText() {
            return String.valueOf(serialNumber);
        }
        @Bindable
        public int getSerialNumberBgRid() {
            return serialNumber > 0 ? serialNumberBgRid[Math.min(3, serialNumber - 1)] : 0;
        }

        @Bindable
        public int getFloatTipsVisible() {
            return StringUtil.isEmpty(moduleItemEntity.floatTips) ? View.GONE : View.VISIBLE;
        }

        @Bindable
        public CharSequence getFloatTips() {
            return moduleItemEntity.floatTips;
        }

        @Bindable
        public CharSequence getTitle() {
            if(StringUtil.isEmpty(moduleItemEntity.title))
                return moduleItemEntity.bookName;
            return moduleItemEntity.title;
        }

        @Bindable
        public CharSequence getShortTitle() {
            return moduleItemEntity.shortTitle;
        }
    }

}
